<template>
  <div class="container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>WebSocket 配置</span>
        </div>
      </template>
      <el-form :model="form" label-width="120px">
        <el-form-item label="Host">
          <el-input v-model="form.host" />
        </el-form-item>
        <el-form-item label="Port">
          <el-input v-model="form.port" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="gotoPath('/home')" class="return-button">返回</el-button>
    </el-card>
  </div>
</template>

<script setup name="WebSocketConfig">
import { ref, onMounted } from 'vue'
import { gotoPath } from '@renderer/api'

const { ipcRenderer } = window.electron

const form = ref({
  host: '',
  port: ''
})

const onSubmit = () => {
  ipcRenderer.invoke('updateWsConfig', { ...form.value }).then(() => {
    console.log('WebSocket config updated')
  })
}

onMounted(() => {
  ipcRenderer.invoke('getWsConfig').then((config) => {
    form.value = config.data
  })
})
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 使容器高度占满整个视口 */
}

.box-card {
  width: 400px;
  margin: 0 auto; /* 确保卡片水平居中 */
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.return-button {
  margin-top: 20px;
  display: block;
  width: 100%;
}
</style>
